<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../../common/Common.php");
require_once(dirname(__FILE__) . "/../../dbcom/WorkerCom.php");

$usercom = new UserCom();
$ragioncom = new RagionCom();
$common = new Common();
$workercom = new WorkerCom();

$userid = $common->getLogin();
if (!isset($userid) || empty($userid)) {
    header('Location:./../login.php');
}
//获取分页
if (empty($_REQUEST['p']) || !isset($_REQUEST['p'])) {
    $page = 1;
    $minlimit = 0;
} else {
    $page = $_REQUEST['p'];
    if ($page > 1) {
        $minlimit = 50 * $page;
    } else {
        $minlimit = 0;
    }
}
$limitnum = 50;

$userarr = $usercom->getUserlist($minlimit, $limitnum);
$number = $usercom->getAllNum();

$commounitylist = $ragioncom->getRagionByType(2);

$p = (int) ($number / 50 + 1);

$res = array();
if (!empty($userarr)) {
    foreach ($userarr as $k => $v) {
        $res[$k]['id'] = $v['id'];
        $res[$k]['account'] = $v['account'];
        //$community = $ragioncom->getRagionById($v['community_id']);
        $communityarr = explode(",", $v['community_id']);
        $res[$k]['community'] = "";
        foreach ($communityarr as $vv) {
            $community = array();
            $community = $ragioncom->getRagionById($vv);
            if (!empty($community) && isset($community['name'])) {
                $res[$k]['community'] .= $community['name'] . ",";
            }
        }
        //$res[$k]['community'] = $community['name'];
        if ($v['role'] == 0) {
            $res[$k]['type'] = "管理员";
        } else {
            $res[$k]['type'] = "工作人员";
        }
    }
}
?>
<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="keywords" content="">
        <meta name="description" content="">
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">

        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">

        <link href="../public/css/paging.css" rel="stylesheet">
        <link href="../public/css/showproject.css" rel="stylesheet">
    </head>

    <body class="gray-bg">

        <div class="wrapper wrapper-content animated fadeInUp">
            <div class="row">
                <div class="col-sm-12">

                    <div class="ibox">
                        <div class="ibox-title">
                            <h5>后台账号管理</h5>
                            <div class="ibox-tools">
                                <a id="addnew" data-toggle="modal" data-target="#myModal1" class="btn btn-primary btn-xs">新增</a>
                            </div>
                        </div>
                        <div class="ibox-content">
                            <div class="row m-b-sm m-t-sm">
                                <div class="col-md-1">

                                </div>
                                <div class="col-md-11">
                                    <div class="input-group">

                                    </div>
                                </div>
                            </div>

                            <div class="project-list">

                                <div class="project-list">
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <td class="project-title">
                                                        <span>名称</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>类型</span>
                                                    </td>
                                                    <td class="project-people">
                                                        <span>所属社区</span>
                                                    </td>
                                                    <td class="project-actions">
                                                        <span>操作</span>
                                                    </td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                foreach ($res as $v) {
                                                    ?>
                                                    <tr id="list<?= $v['id'] ?>" data-id="<?= $v['id'] ?>">
                                                        <td class="project-title">
                                                            <span><?= $v['account'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['type'] ?></span>
                                                        </td>
                                                        <td class="project-people">
                                                            <span><?= $v['community'] ?></span>
                                                        </td>
                                                        <td class="project-actions">
                                                            <a data-id="<?= $v['id'] ?>" data-toggle="modal" data-target="#myModal2" class="btn btn-white btn-sm btn-edt"><i class="fa fa-folder"></i> 编辑 </a>
                                                            <a data-id="<?= $v['id'] ?>" class="btn btn-white btn-sm btn-det"><i class="fa fa-folder"></i> 删除 </a>
                                                        </td>
                                                    </tr>
                                                <?php } ?>
                                            </tbody>
                                        </table>
                                        <div class="box" id="box"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" id="close1" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">新增后台账号</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>分类选择</label>
                            <select class="form-control" id="type" style="height: 35px;">
                                <option value="1" >工作人员</option>
                                <option value="0" >管理员</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>登录用户名</label>
                            <input type="text" placeholder="" id="username" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>登录密码</label>
                            <input type="text" placeholder="" id="password" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>权限选择(分类为工作人员时)</label>

                            <div class="col-sm-10">
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="1" name="role">客户列表</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="2" name="role">使用者类型</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="3" name="role">地区</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="4" name="role">预约管理</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="5" name="role">工作人员</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="6" name="role">预约时间</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="7" name="role">服务管理</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="8" name="role">机械列表</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="9" name="role">统计图表</label>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>社区选择(分类为工作人员时)</label>

                            <div class="col-sm-10">
                                <?php foreach ($commounitylist as $v) { ?>
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="<?= $v['id'] ?>" name="communityid"><?= $v['name'] ?></label>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="addworker" class="btn btn-info">保存</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>

        <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <input type="hidden" value="" id="workerid"/>
                    <div class="modal-header">
                        <button type="button" class="close" id="close1" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">社区</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>分类选择</label>
                            <select class="form-control" id="oldtype" style="height: 35px;">
                                <option value="1" >工作人员</option>
                                <option value="0" >管理员</option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label>用户名</label>
                            <input type="text" disabled="" placeholder="" id="oldaccount" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>密码</label>
                            <input type="text" placeholder="如果您不需要修改密码,请不要填写此项！" id="oldpassword" class="form-control">
                        </div>
                        <div class="form-group">
                            <label>权限选择(分类为工作人员时)</label>

                            <div class="col-sm-10">
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="1" id="oldrole1" name="oldrole">客户列表</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="2" id="oldrole2" name="oldrole">使用者类型</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="3" id="oldrole3" name="oldrole">地区</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="4" id="oldrole4" name="oldrole">预约管理</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="5" id="oldrole5" name="oldrole">工作人员</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="6" id="oldrole6" name="oldrole">预约时间</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="7" id="oldrole7" name="oldrole">服务管理</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="8" id="oldrole8" name="oldrole">机械列表</label>
                                <label class="checkbox-inline">
                                    <input type="checkbox" value="9" id="oldrole9" name="oldrole">统计图表</label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>社区选择(分类为工作人员时)</label>

                        <div class="col-sm-10">
                            <?php foreach ($commounitylist as $v) { ?>
                                <label class="checkbox-inline">
                                    <input type="checkbox" id="oldcommunity<?= $v['id'] ?>" value="<?= $v['id'] ?>" name="oldcommunity"><?= $v['name'] ?></label>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="mychange" class="btn btn-info">修改</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>

        <!-- 全局js -->
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>

        <!-- 自定义js -->
        <script src="../public/js/paging.js?v=1.0.0"></script>
        <script src="../public/js/upload.js?v=1.0.0"></script>
        <script src="../public/js/common.js?v=1.0.0"></script>

        <script>
            $(document).ready(function () {

                $('#loading-example-btn').click(function () {
                    btn = $(this);
                    simpleLoad(btn, true)

                    // Ajax example
                    //                $.ajax().always(function () {
                    //                    simpleLoad($(this), false)
                    //                });

                    simpleLoad(btn, false)
                });
            });

            function simpleLoad(btn, state) {
                if (state) {
                    btn.children().addClass('fa-spin');
                    btn.contents().last().replaceWith(" Loading");
                } else {
                    setTimeout(function () {
                        btn.children().removeClass('fa-spin');
                        btn.contents().last().replaceWith(" Refresh");
                    }, 2000);
                }
            }
            var setTotalCount = <?= $number ?>;
            $('#box').paging({
                initPageNo: <?= $page ?>, // 初始页码
                totalPages: <?= $p ?>, //总页数
                totalCount: '合计' + setTotalCount + '条数据', // 条目总数
                slideSpeed: 600, // 缓动速度。单位毫秒
                jump: true, //是否支持跳转
                callback: function (page) { // 回调函数
                    console.log(page);
                }
            });

            $("#addworker").click(function () {
                var username = $("#username").val();
                var password = $("#password").val();
                var type = $("#type").val();
                if (username == "" || password == "" || type == 　"") {
                    layer.msg('请填写所需内容', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                var datastr = "cont=1&account=" + username + "&password=" + password + "&type=" + type;
                if (type != 0) {
                    var community = "";
                    var carr = new Array();
                    $('[name=communityid]:checkbox:checked').each(function () {
                        carr.push($(this).val());
                    });
                    community = carr.join(",");
                    var rolestr = "";
                    var rarr = new Array();
                    $('[name=role]:checkbox:checked').each(function () {
                        rarr.push($(this).val());
                    });
                    rolestr = rarr.join(",");
                    datastr += "&community=" + community + "&role=" + rolestr;
                    if (community == "" || rolestr == "") {
                        layer.msg('请填写所需内容', {
                            time: 2000, //20s后自动关闭
                            btn: ['OK']
                        });
                        return;
                    }
                }
                //console.log(datastr);
                $.ajax({
                    url: "/../../controller/user.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        var result = eval("(" + e + ")");
                        if (result.code == 100) {
                            layer.msg('用户名已被占用', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        if (result.code == 0) {
                            window.location.reload();
                        } else {
                            layer.msg('添加失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                    }
                });
            });
            $(".btn-det").click(function () {
                var workerid = $(this).data("id");
                var datastr = "cont=2&userid=" + workerid;
                $.ajax({
                    url: "/../controller/user.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            layer.msg('删除成功', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            $("#list" + result.data).css("display", "none");
                        } else {
                            layer.msg('删除失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
            $(".btn-edt").click(function () {
                $("#oldaccount").val("");
                $("#oldpassword").val("");
                $("#workerid").val("");
                $("input[name=oldcommunity]").each(function () {
                    $(this).prop("checked", false);
                });
                $("input[name=oldrole]").each(function () {
                    $(this).prop("checked", false);
                });
                var workerid = $(this).data("id");
                var datastr = "cont=4&userid=" + workerid;
                $.ajax({
                    url: "/../../controller/user.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        //console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            $("#workerid").val(workerid);
                            //$("#oldcommunityid").val(result.data.community);
                            $("#oldaccount").val(result.data.account);
                            if (result.data.role == 0) {
                                $("#oldtype").val(0);
                                $("input[name=oldrole]").each(function () {
                                    $(this).prop("checked", true);
                                });
                            } else {
                                $("#oldtype").val(1);
                                var rarr = result.data.role.split(",");
                                $(rarr).each(function (k, v) {
                                    $("#oldrole" + v).prop('checked', true);
                                });
                                var carr = result.data.community.split(",");
                                $(carr).each(function (k, v) {
                                    $("#oldcommunity" + v).prop('checked', true);
                                });
                            }
                        } else {
                            layer.msg('获取失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
            $("#mychange").click(function () {
                //var community = $("#oldcommunityid").val();
                var workerid = $("#workerid").val();
                var type = $("#oldtype").val();
                if (workerid == "" || type == "") {
                    layer.msg('更新失败请刷新重试', {
                        time: 2000, //20s后自动关闭
                        btn: ['OK']
                    });
                    return;
                }
                var password = $("#oldpassword").val();
                var datastr = "cont=3&userid=" + workerid + "&type=" + type;
                if (password != "") {
                    datastr += "&password=" + password;
                }
                if (type != 0) {
                    var rolestr = "";
                    var rarr = new Array();
                    $('[name=oldrole]:checkbox:checked').each(function () {
                        rarr.push($(this).val());
                    });
                    rolestr = rarr.join(",");
                    var community = "";
                    var rarr = new Array();
                    $('[name=oldcommunity]:checkbox:checked').each(function () {
                        rarr.push($(this).val());
                    });
                    community = rarr.join(",");
                    datastr += "&community=" + community + "&role=" + rolestr;
                }
                //console.log(datastr);
                $.ajax({
                    url: "/../../controller/user.php",
                    type: "POST",
                    data: datastr,
                    success: function (e) {
                        console.log(e);
                        var result = eval("(" + e + ")");
                        if (result.code == 0) {
                            window.location.reload();
                        } else {
                            layer.msg('更新失败请刷新重试', {
                                time: 2000, //20s后自动关闭
                                btn: ['OK']
                            });
                            return;
                        }
                        return;
                    }
                });
            });
        </script>

    </body>
</html>
